package com.example.composewanandroid.ui.screens.home.explore

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.focusModifier
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.example.composewanandroid.R
import com.example.composewanandroid.navigation.LocalNavController
import com.example.composewanandroid.navigation.RoutePath
import com.example.composewanandroid.network.model.ArticleInfo
import com.example.composewanandroid.util.RegexUtils

@Composable
fun ExploreScreen(viewModel: ExploreViewModel = hiltViewModel()) {
    val navHostController = LocalNavController.current
    val articleList by viewModel.articleList.collectAsStateWithLifecycle()
    val bannerList by viewModel.bannerList.collectAsStateWithLifecycle()

    LaunchedEffect(Unit) {
        viewModel.getHomeBanner()//请求Banner数据
        viewModel.getHomeInfoList(20)//请求首页数据
    }

    LazyColumn {
        item {
            Text("搜索关键词以空格形式隔开", modifier = Modifier.background(colorResource(R.color.white), shape = RoundedCornerShape(20.dp)).padding(2.dp).clickable {
                navHostController.navigate( RoutePath.ARTICLE_SEARCH + "/111")

            })
        }
        item {
            Banner(bannerList) { link ->
                //进行页面跳转，携带参数
                navHostController.navigate("${RoutePath.web}?url=$link")

            }
        }
        articleList?.size?.let {
            items(it) { index ->
                ExploreItem(articleList?.datas?.get(index))
                Spacer(modifier = Modifier.height(20.dp))
            }
        }
    }
}

@Composable
fun ExploreItem(get: ArticleInfo?) {
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .background(colorResource(R.color.teal_200), shape = RoundedCornerShape(10.dp))
    ) {
        Row(
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(
                text = get?.author!!, modifier = Modifier
                    .padding(3.dp)
                    .background(
                        colorResource(R.color.purple_200), shape = RoundedCornerShape(1000.dp)
                    )
                    .padding(10.dp)
            )
            Text(text = get?.title!!, modifier = Modifier.weight(1f))
            Text(
                text = RegexUtils().timestamp(get.niceDate!!) ?: "1970-1-1",
            )
            Spacer(Modifier.width(3.dp))


        }
        Column {
            get?.desc?.let { Text(it, modifier = Modifier.padding(10.dp)) }

        }
        LazyRow(modifier = Modifier.padding(start = 10.dp)) {
            get?.tags?.let {
                items(2) { data ->
                    Text(
                        data.toString(),
                        modifier = Modifier
                            .background(
                                colorResource(R.color.purple_200),
                                shape = RoundedCornerShape(1000.dp)
                            )
                            .padding(horizontal = 10.dp, vertical = 3.dp)
                    )
                    Spacer(Modifier.width(3.dp))

                }
            }

        }
    }

}

@Preview
@Composable
fun ExploreItemPreview() {
    ExploreItem(
        ArticleInfo(
            1,
            1,
            1,
            1,
            false,
            "asdfasdf",
            "asdfasdfasdfasdfasdf",
            "asdfasdfasdfasdfasdf",
            1,
            "niceShareDate: String?",
            "niceDate: String",
            1,
            "shareUser: String?",
            "",
            "",
            "chapterName: String?",
            arrayListOf("广场tab", "广场"),
        )
    )
}
